package com.twentyfour.controller;

import com.twentyfour.pojo.*;
import com.twentyfour.service.AdminServiceImpl;
import com.twentyfour.service.LeaderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

@Controller
public class LeaderController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;

    @Autowired
    @Qualifier("LeaderServiceImpl")
    LeaderServiceImpl leaderService;


    @RequestMapping("/addAthlete")
    public void addAthlete(HttpSession session, HttpServletResponse response, HttpServletRequest request,
                           String name, Integer age, String sex,Integer athNum) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        Athlete athlete = new Athlete();
        athlete.setName(name);
        athlete.setAge(age);
        athlete.setSex(sex);
        athlete.setAthNum(athNum);

        int random = new Random().nextInt(90)+10;
        if (sex.equals("男")) {
            if (random % 2 == 0) {
                random += 1;
                athlete.setAthNum(random);
            }

        } else if (sex.equals("女")) {
            if (random % 2 == 1) {
                random += 1;
                athlete.setAthNum(random);
            }
        }

        athlete.setTeamAccount(certainTeamId);

        leaderService.addAthlete(athlete);

        response.sendRedirect(request.getContextPath()+"/toAthleteList?certainTeamId="+certainTeamId);
    }

    @RequestMapping("/deleteAthlete")
    public void deleteAthlete(HttpSession session, HttpServletResponse response, HttpServletRequest request,
                              @RequestParam("athleteId")Integer athleteId) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        leaderService.deleteAthlete(athleteId);



        response.sendRedirect(request.getContextPath()+"/toAthleteList?certainTeamId="+certainTeamId);
    }

    @RequestMapping("/addAthleteItem")
    public void addAthleteItem(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                               Integer itemId,Integer athleteId) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        Athlete_Item athlete_item = new Athlete_Item();
        athlete_item.setItemId(itemId);
        athlete_item.setAthleteId(athleteId);

        leaderService.addAthleteItem(athlete_item);

        response.sendRedirect(request.getContextPath()+"/toAthleteList?certainTeamId="+certainTeamId);
    }

    @RequestMapping("/addCoach")
    public void addCoach(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                         String name,String  phoneNum,String sex) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        Coach coach = new Coach();
        coach.setName(name);
        coach.setPhoneNum(phoneNum);
        coach.setSex(sex);
        coach.setTeamAccount(certainTeamId);

        leaderService.addCoach(coach);

        response.sendRedirect(request.getContextPath()+"/toCoachList?certainTeamId="+certainTeamId);

    }

    @RequestMapping("/deleteCoach")
    public void deleteCoach(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                            Integer coachId) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        leaderService.deleteCoach(coachId);
        response.sendRedirect(request.getContextPath()+"/toCoachList?certainTeamId="+certainTeamId);
    }

    @RequestMapping("/addDoctor")
    public void addDoctor(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                         String name,String  phoneNum) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        Doctor doctor = new Doctor();
        doctor.setName(name);
        doctor.setPhoneNum(phoneNum);
        doctor.setTeamAccount(certainTeamId);


        leaderService.addDoctor(doctor);

        response.sendRedirect(request.getContextPath()+"/toDoctorList?certainTeamId="+certainTeamId);

    }

    @RequestMapping("/deleteDoctor")
    public void deleteDoctor(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                            Integer doctorId) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        leaderService.deleteDoctor(doctorId);
        response.sendRedirect(request.getContextPath()+"/toDoctorList?certainTeamId="+certainTeamId);
    }

    @RequestMapping("/addReferee")
    public void addReferee(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                           String name,String  phoneNum,Integer account) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        Referee referee = new Referee();
        referee.setName(name);
        referee.setPhoneNum(phoneNum);
        referee.setAccount(account);
        referee.setTeamAccount(certainTeamId);

        leaderService.addReferee(referee);
        response.sendRedirect(request.getContextPath()+"/toRefereeList?certainTeamId="+certainTeamId);

    }

    @RequestMapping("/deleteReferee")
    public void deleteReferee(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                             Integer refereeId) throws IOException {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");

        leaderService.deleteReferee(refereeId);
        response.sendRedirect(request.getContextPath()+"/toRefereeList?certainTeamId="+certainTeamId);
    }
}
